﻿using UnityEngine;
using System.IO;
using System.Text;
using System;
using System.Collections.Generic;

namespace TinyAsset
{
    public static class FileHelper
    {

        public static readonly string[] searchPath = new string[] {
#if UNITY_EDITOR
        AssetBundleUtility.kUseAssetDatabase ? Application.dataPath : null,
#endif
        Application.persistentDataPath,

#if !UNITY_ANDROID || UNITY_EDITOR
        Application.streamingAssetsPath,
#endif
    };

        public static byte[] ReadAllBytes(string path)
        {
            byte[] bytes = null;

            for (int i = 0; i < searchPath.Length; i++)
            {
                if (string.IsNullOrEmpty(searchPath[i]))
                    continue;

                string filePath = Path.Combine(searchPath[i], path);
                if (File.Exists(filePath))
                {
                    bytes = File.ReadAllBytes(filePath);
                    break;
                }
            }

#if UNITY_ANDROID && !UNITY_EDITOR
        if(bytes == null)
            bytes = AndroidUtility.GetAsset(path);
#endif
            return bytes;
        }

        public static string ReadAllText(string path)
        {
            byte[] bytes = ReadAllBytes(path);
            if (bytes == null)
                return null;
            return System.Text.Encoding.Default.GetString(bytes);
        }

        public static string GetFileMd5(string filePath)
        {
            string hash = null;
            using (FileStream fileStream = File.OpenRead(filePath))
            {
                hash = ComputeMd5Hash(fileStream);
            }
            return hash;
        }

        public static string ComputeMd5Hash(Stream inputStream)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = md5.ComputeHash(inputStream);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
                sb.Append(bytes[i].ToString("x2"));
            return sb.ToString();
        }

        public static void Copy(string source, string destination, bool overwrite = true)
        {
            var dir = Path.GetDirectoryName(destination);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            File.Copy(source, destination, overwrite);
        }

        public static void CopyFolder(string scource, string destination, HashSet<string> excludeExtensions, bool overwrite = true)
        {
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);

            string[] files = Directory.GetFiles(scource);
            foreach (var file in files)
            {
                string ext = Path.GetExtension(file);
                if (excludeExtensions != null && excludeExtensions.Contains(ext))
                    continue;
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destination, name);
                File.Copy(file, dest, overwrite);
            }

            string[] folders = Directory.GetDirectories(scource);
            foreach (var folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destination, name);
                CopyFolder(folder, dest, excludeExtensions, overwrite);
            }
        }
    }
}